<template>
  <div class="mw-modify">
    <el-dialog :visible.sync="dialogVisible.show" center>
      <el-form :inline="true" :model="ruleform" ref="from" :rules="rules">
        <div class="con">
          <div class="dirver">平台为网关配置的参数：</div>
          <el-form-item label="用户名" prop="user_for_gateway">
            <el-input
              v-model="ruleform.user_for_gateway"
              autocomplete="off"
              disabled
            ></el-input>
          </el-form-item>
          <el-form-item label="设备编码" prop="device_code_for_gateway">
            <el-input
              v-model="ruleform.device_code_for_gateway"
              placeholder
            ></el-input>
          </el-form-item>
          <el-form-item label="初始令牌" prop="token_for_gateway">
            <el-input
              v-model="ruleform.token_for_gateway"
              style="width:430px"
              placeholder
              readonly
            ></el-input>
          </el-form-item>
          <br />
          <el-form-item label="SM2公钥串" prop="public_key_for_gateway">
            <el-input
              class="textarea-gateway"
              v-model="ruleform.public_key_for_gateway"
              type="textarea"
              :autosize="{ minRows: 1.5, maxRows: 4 }"
              placeholder
            ></el-input>
          </el-form-item>
          <el-form-item label="SM2私钥串" prop="private_key_for_gateway">
            <el-input
              class="textarea-gateway"
              v-model="ruleform.private_key_for_gateway"
              type="textarea"
              :autosize="{ minRows: 1.5, maxRows: 4 }"
              placeholder
            ></el-input>
          </el-form-item>

          <div class="dirver">网关为平台提供的参数：</div>
          <el-form-item label="用户名" prop="user_of_gateway">
            <el-input v-model="ruleform.user_of_gateway" placeholder></el-input>
          </el-form-item>
          <el-form-item label="设备编码" prop="device_code_of_gateway">
            <el-input
              v-model="ruleform.device_code_of_gateway"
              placeholder
            ></el-input>
          </el-form-item>
          <el-form-item label="初始令牌" prop="token_of_gateway">
            <el-input
              v-model="ruleform.token_of_gateway"
              style="width:280px"
              placeholder
            ></el-input>
          </el-form-item>
          <el-form-item label="SM2私钥串" prop="private_key_of_gateway">
            <el-input
              class="textarea-gateway"
              v-model="ruleform.private_key_of_gateway"
              type="textarea"
              :autosize="{ minRows: 1.5, maxRows: 4 }"
              placeholder
            ></el-input>
          </el-form-item>

          <div class="dirver">其他参数：</div>
          <el-form-item label="网关别名" prop="alias">
            <el-input v-model="ruleform.alias" placeholder></el-input>
          </el-form-item>
          <el-form-item label="签名方式" prop="sign_mode">
            <el-select v-model="ruleform.sign_mode" placeholder="请选择">
              <el-option label="SM2" value="SM2"></el-option>
              <el-option label="MD5" value="MD5"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="sip服务模式" prop="sip_mode">
            <el-select v-model="ruleform.sip_mode" placeholder="请选择">
              <el-option label="混合模式" :value="0"></el-option>
              <el-option label="独立模式" :value="1"></el-option>
              <el-option label="高速特有模式" :value="2"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="服务地址" prop="service_uri">
            <el-input
              v-model="ruleform.service_uri"
              placeholder
              style="width:380px"
            ></el-input>
          </el-form-item>

          <div slot="footer" class="dialog-footer">
            <el-button type="primary" @click="submitModify('from')"
              >修改</el-button
            >
            <el-button type="warning" plain @click="onReset('from')"
              >重置</el-button
            >
          </div>
        </div>
      </el-form>
    </el-dialog>
  </div>
</template>

<script>
import { getData, postData } from "@/request/http";
export default {
  name: "audit",
  props: {
    user_for_gateway: {
      type: String,
      default: "",
    },
    dialogVisible: {
      type: Object,
      default() {
        return {
          show: false,
        };
      },
    },
  },
  data() {
    // var checkNoChinese = (rule, value, callback) => {
    //   if (value) {
    //     if (new RegExp(/^[\u4E00-\u9FA5]+$/).test(value)) {
    //       callback(new Error("请不要输入中文！"));
    //     } else {
    //       callback();
    //     }
    //   }
    //   callback();
    // };
    // var checkNumber = (rule, value, callback) => {
    //   if (value != "") {
    //     if (/^[+]{0,1}(\d+)$|^[+]{0,1}(\d+\.\d+)$/.test(value) == false) {
    //       callback(new Error("请输入大于0的数字"));
    //     } else {
    //       callback();
    //     }
    //   } else {
    //     callback();
    //   }
    //   callback();
    // };
    return {
      ruleform: {
        user_for_gateway: "",
        device_code_for_gateway: "",
        public_key_for_gateway: "",
        private_key_for_gateway: "",
        user_of_gateway: "",
        device_code_of_gateway: "",
        token_of_gateway: "",
        private_key_of_gateway: "",
        sign_mode: "",
        sip_mode: "",
        service_uri: "",
        alias: "",
      },
      rules: {
        user_for_gateway: [{ required: false, message: "", trigger: "blur" }],
        device_code_for_gateway: [
          { required: false, message: "", trigger: "blur" },
        ],
        public_key_for_gateway: [
          { required: false, message: "", trigger: "blur" },
        ],
        private_key_for_gateway: [
          { required: false, message: "", trigger: "blur" },
        ],
        user_of_gateway: [{ required: false, message: "", trigger: "blur" }],
        device_code_of_gateway: [
          { required: false, message: "", trigger: "blur" },
        ],
        token_of_gateway: [{ required: false, message: "", trigger: "blur" }],
        private_key_of_gateway: [
          { required: false, message: "", trigger: "blur" },
        ],
        sign_mode: [{ required: false, message: "", trigger: "blur" }],
        sip_mode: [{ required: false, message: "", trigger: "blur" }],
        service_uri: [{ required: false, message: "", trigger: "blur" }],
        alias: [{ required: false, message: "", trigger: "blur" }],
      },
    };
  },
  watch: {
    user_for_gateway: {
      handler(newVal) {
        this.ruleform.user_for_gateway = newVal;
        this.initShow();
      },
      immediate: true,
    },
  },
  mounted() {
    // this.initShow();
  },
  methods: {
    //初始的时候显示,
    initShow() {
      postData("/adminweb/mwgateway/get", {
        user_for_gateway: this.user_for_gateway,
      })
        .then((res) => {
         // console.log(res);
          if (res.code == 0) {
            let datas = res.datas;
            this.ruleform.user_for_gateway = datas.user_for_gateway;
            this.ruleform.device_code_for_gateway =
              datas.device_code_for_gateway;
            this.ruleform.token_for_gateway = datas.token_for_gateway;
            this.ruleform.public_key_for_gateway = datas.public_key_for_gateway;
            this.ruleform.private_key_for_gateway =
              datas.private_key_for_gateway;
            this.ruleform.user_of_gateway = datas.user_of_gateway;
            this.ruleform.device_code_of_gateway = datas.device_code_of_gateway;
            this.ruleform.token_of_gateway = datas.token_of_gateway;
            this.ruleform.private_key_of_gateway = datas.private_key_of_gateway;
            this.ruleform.sign_mode = datas.sign_mode;
            this.ruleform.sip_mode = datas.sip_mode;
            this.ruleform.service_uri = datas.service_uri;
            this.ruleform.alias = datas.alias;
          } else if (res.code == -56055) {
            return;
          } else {
            this.$message.error(res.code + "，" + res.msg);
          }
        })
        .catch((res) => {
          this.$message.error(res.code + "，" + res.msg);
        });
    },
    //修改 提交
    submitModify(formName) {
      var that = this;
      that.$refs[formName].validate((valid) => {
        if (valid) {
          postData("/adminweb/mwgateway/update", {
            user_for_gateway: that.ruleform.user_for_gateway,
            device_code_for_gateway: that.ruleform.device_code_for_gateway,
            public_key_for_gateway: that.ruleform.public_key_for_gateway,
            private_key_for_gateway: that.ruleform.private_key_for_gateway,
            user_of_gateway: that.ruleform.user_of_gateway,
            device_code_of_gateway: that.ruleform.device_code_of_gateway,
            token_of_gateway: that.ruleform.token_of_gateway,
            private_key_of_gateway: that.ruleform.private_key_of_gateway,
            sign_mode: that.ruleform.sign_mode,
            sip_mode: that.ruleform.sip_mode,
            service_uri: that.ruleform.service_uri,
            alias: that.ruleform.alias,
          })
            .then((res) => {
              if (res.code == 0) {
                that.$message.success("修改成功！");
                that.$emit("refreshTableList");
              } else {
                that.$message.error(res.code + "，" + res.msg);
              }
            })
            .catch((res) => {
              that.$message.error(res.code + "，" + res.msg);
            });
        } else {
          that.$message.error("修改失败");
          return false;
        }
      });
    },
    // 重置
    onReset(val) {
      //this.$emit("reset", true);
      this.$refs[val].resetFields();
    },
  },
};
</script>

<style scoped>
.mw-modify >>> .el-dialog__wrapper {
}
.dirver {
  margin-bottom: 20px;
  height: 40px;
  line-height: 40px;
  border-bottom: 1px solid #a8bddc;
  /* background: #e8eef7;
  border-radius: 12px 12px 0 0; */
  font-weight: 700;
  font-size: 14px;
}
.mw-modify >>> .textarea-gateway textarea {
  /* margin-top: -20px; */
  width: 250px;
}

.mw-modify >>> .el-dialog__body {
  padding: 0 20px 20px 20px !important;
}
</style>
